package tgc.edu.tms.web.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.tms.custom.AjaxResult;
import tgc.edu.tms.custom.CommonController;
import tgc.edu.tms.custom.ExportExcel;
import tgc.edu.tms.custom.TimeUtils;
import tgc.edu.tms.entity.ClassSchedule;
import tgc.edu.tms.entity.ClassType;
import tgc.edu.tms.entity.Classroom;
import tgc.edu.tms.entity.CourseCategory;
import tgc.edu.tms.entity.EducationalPlanner;
import tgc.edu.tms.entity.FlowTable;
import tgc.edu.tms.entity.Jcb;
import tgc.edu.tms.entity.Profession;
import tgc.edu.tms.entity.Schedule;
import tgc.edu.tms.entity.Schoolyear;
import tgc.edu.tms.entity.SysUser;
import tgc.edu.tms.entity.TbClass;
import tgc.edu.tms.entity.Teacher;
import tgc.edu.tms.entity.Xqsj;
import tgc.edu.tms.security.UserUtils;
import tgc.edu.tms.service.ClassroomService;
import tgc.edu.tms.service.CourseCategoryService;
import tgc.edu.tms.service.EducationalPlannerService;
import tgc.edu.tms.service.FlowTableService;
import tgc.edu.tms.service.JcbService;
import tgc.edu.tms.service.ProfessionService;
import tgc.edu.tms.service.ScheduleService;
import tgc.edu.tms.service.SchoolyearService;
import tgc.edu.tms.service.TbClassService;
import tgc.edu.tms.service.TeacherService;
import tgc.edu.tms.service.XqsjService;
import tgc.edu.tms.web.form.EducationalPlannerForm;
import tgc.edu.tms.web.form.ScheduleForm;

@Controller
@RequestMapping(value = "/schedule")
public class ScheduleController extends CommonController<Schedule, Integer, ScheduleForm> {
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private ProfessionService proservice;
	@Autowired
	private ClassroomService classroomService;
	@Autowired
	private JcbService jcbService;
	@Autowired
	private TbClassService tbClassService;
	@Autowired
	private SchoolyearService schoolyerarService;
	@Autowired
	private ScheduleService scheduleService;
	@Autowired
	private EducationalPlannerService educationalPlannerService;
	@Autowired
	private FlowTableService flowTableService;
	@Autowired
	private XqsjService xqsjService;
	@Autowired
	private TeacherService teacherService;
	@Autowired
	private ProfessionService professionService;// 学年 学院 专业
	@Autowired
	private CourseCategoryService courseCategoryService;// 课程类别

	// 获取所有教室数据
	@RequestMapping(value = "/classroom")
	@ResponseBody
	public Object classroom() {
		List<Classroom> list = classroomService.findAll();
		return list;
	}

	// 上课节次
	@RequestMapping(value = "/getSkjc")
	@ResponseBody
	public Object getSkjc(String skxq) {
		List<Jcb> list = jcbService.findBySkxq(skxq);
		return list;
	}

	// 获取班级
	@RequestMapping(value = "/tbclass")
	@ResponseBody
	public Object tbclass(Integer id) {
		List<TbClass> list = tbClassService.findByprofessionID(id);
		return list;
	}

	// 课程计划和学期
	@RequestMapping(value = "/kcjhandxq")
	@ResponseBody
	private Object kcjhandxq(String year,String college,String profesion, String schoolyear) {
		Integer schoolyearId = schoolyerarService.findByXnxq(schoolyear).getId();
		Profession profession = professionService.findByYearAndCollegeAndProfesion(year, college, profesion);
		List<EducationalPlanner> ep = educationalPlannerService
				.findBySchoolyearIdAndCurriculumProfessionId(schoolyearId, profession.getId());
		return ep;
	}

	// 课程计划和学期,按老师查找
	@RequestMapping(value = "/kcjhandxq1")
	@ResponseBody
	private Object kcjhandxq1(String year,String college,String profesion, String schoolyear) {
		SysUser user = userUtils.getUser();
		Integer schoolyearId = schoolyerarService.findByXnxq(schoolyear).getId();
		Profession profession = professionService.findByYearAndCollegeAndProfesion(year, college, profesion);
		List<EducationalPlanner> ep = educationalPlannerService
				.findBySchoolyearIdAndCurriculumProfessionIdAndTeacherUsername(schoolyearId, profession.getId(), user.getUsername());
		return ep;
	}

	@RequestMapping(value = "/tjkc")
	public String tjkc(ModelMap map) {
		SysUser user = userUtils.getUser();
		String xnxq = TimeUtils.getxq();
		Schedule model = new Schedule();
		model = null;
		List<Schedule> tbclass = scheduleService.findByTeacherAndXnxq(user.getName(), xnxq);
		List<String> years = proservice.findAllYear();
		List<String> findAllXq = jcbService.findAllXq();
		List<CourseCategory> courseCategorys = courseCategoryService.findAll();
		
		map.put("courseCategorys", courseCategorys);
		map.put("xq", findAllXq);
		map.put("years", years);
		map.put("kc", tbclass);
		map.put("model", model);
		return "classSchedule/tjkc";
	}

	@RequestMapping(value = "/xnxq")
	@ResponseBody
	public Object xnxq() {
		List<Schoolyear> list = schoolyerarService.findAll();
		return list;
	}

	@Override
	public Object save(ScheduleForm form) {
		try {
			Schedule model = new Schedule();
			Integer id = form.getId();
			if (id != null) {
				model = scheduleService.findById(id);
			}
			model.setXnxq(form.getXnxq());
			Classroom classroom = classroomService.findBySerialNumber(form.getSerialNumber());
			model.setLocation(classroom.getLocation());
			model.setFloor(classroom.getFloor());
			model.setSerialNumber(classroom.getSerialNumber());
			model.setCapacity(classroom.getCapacity());
			model.setType(classroom.getType());
			model.setXq(form.getSkxq());
			model.setJcsj(form.getSkjc());
			model.setKcmc(form.getKcmc());
			model.setQszc(form.getQszc());
			model.setJszc(form.getJszc());
			model.setTeacher(form.getTeacher());
			model.setZongxs(form.getZongxs());
			model.setXmh(form.getXmh());
			Profession profession = professionService.findByYearAndCollegeAndProfesion(form.getYear(), form.getCollege(), form.getProfesion());
			model.setProfession(profession);
			String bj = "";
			for (String a : form.getSkbj()) {
				bj += a + ",";
			}
			model.setTbclass(bj.substring(0, bj.length() - 1));
			String kclb = "";
			for (String a : form.getKclb()) {
				kclb += a + ",";
			}
			model.setKclb(kclb.substring(0, kclb.length() - 1));
			model.setKkxq(form.getKkxq());
			scheduleService.save(model);
			return new AjaxResult("数据保存成功");
		} catch (Exception e) {
			return new AjaxResult(false, "数据保存失败");
		}
	}

	// 老师添加课程
	@RequestMapping(value = "/save1")
	@ResponseBody
	public Object save1(ScheduleForm form) {
		try {
			Schedule model = new Schedule();
			Integer id = form.getId();
			if (id != null) {
				model = scheduleService.findById(id);
			}
			model.setXnxq(form.getXnxq());
			Classroom classroom = classroomService.findBySerialNumber(form.getSerialNumber());
			model.setLocation(classroom.getLocation());
			model.setFloor(classroom.getFloor());
			model.setSerialNumber(classroom.getSerialNumber());
			model.setCapacity(classroom.getCapacity());
			model.setType(classroom.getType());
			model.setXq(form.getSkxq());
			model.setJcsj(form.getSkjc());
			model.setKcmc(form.getKcmc());
			model.setQszc(form.getQszc());
			model.setJszc(form.getJszc());
			model.setTeacher(form.getTeacher());
			model.setZongxs(form.getZongxs());
			model.setXmh(form.getXmh());
			Profession profession = professionService.findByYearAndCollegeAndProfesion(form.getYear(), form.getCollege(), form.getProfesion());
			model.setProfession(profession);
			String bj = "";
			for (String a : form.getSkbj()) {
				bj += a + ",";
			}
			model.setTbclass(bj.substring(0, bj.length() - 1));
			String kclb = "";
			for (String a : form.getKclb()) {
				kclb += a + ",";
			}
			model.setKclb(kclb.substring(0, kclb.length() - 1));
			model.setKkxq(form.getKkxq());
			scheduleService.save(model);
			return new AjaxResult("数据保存成功");
		} catch (Exception e) {
			return new AjaxResult(false, "数据保存失败");
		}
	}

	@Override
	public void edit(ScheduleForm form, ModelMap map) throws InstantiationException, IllegalAccessException {

		Schedule model = new Schedule();
		model = null;
		Integer id = form.getId();
		if (id != null) {
			model = scheduleService.findById(id);
		}
		List<CourseCategory> courseCategorys = courseCategoryService.findAll();
		map.put("courseCategorys", courseCategorys);
		List<String> years = proservice.findAllYear();
		List<String> findAllXq = jcbService.findAllXq();
		map.put("xq", findAllXq);
		map.put("years", years);
		map.put("model", model);
	}

	// 获取所有年级信息
	@RequestMapping(value = "/findAllYear")
	@ResponseBody
	public Object findAllYear() {
		List<String> findAllYear = proservice.findAllYear();
		return findAllYear;
	}
    
	// 获取该年级下所有专业信息
	@RequestMapping(value = "/yearChanges")
	@ResponseBody
	public Object yearChanges(String year) {
		List<String> profesion = proservice.findProfesionByYear(year);
		return profesion;
	}
	
	/**
	 * 根据年级获取学院
	 * 
	 * @param year
	 * @return
	 */
	@RequestMapping(value = "/getCollege", method = RequestMethod.POST)
	@ResponseBody
	public Object getCollege(String year) {
		return professionService.findCollegeByYear(year);
	}
	/**
	 * 根据年级和学院获取专业
	 * 
	 * @param year
	 * @param college
	 * @return
	 */
	@RequestMapping(value = "/getProfesion", method = RequestMethod.POST)
	@ResponseBody
	public Object getProfesion(String year, String college) {
		return professionService.findProfessionByYearAndCollege(college, year);
	}
	
	// 获取所有学年学期
	@RequestMapping(value = "/findAgcsSchoolyear")
	@ResponseBody
	public Object findAgcsSchoolyear() {
		Sort sort = new Sort(Sort.Direction.DESC, "id");
		List<Schoolyear> findAllYear = schoolyerarService.findAll(sort);
		return findAllYear;
	}



	// 获取该年级该专业下所有班级
	@RequestMapping(value = "/majorChange")
	@ResponseBody
	public Object majorChange(String major, String years) {
		Integer proId = proservice.findIdByProfesionAndYear(major, years);
		List<TbClass> findByProfession_id = tbClassService.findByProfession_id(proId);
		return findByProfession_id;
	}

	// 查找所有学年学期
	@RequestMapping(value = "/findSchoolYear")
	@ResponseBody
	public Object schoolyear(String year) {
		Integer years = Integer.parseInt(year);// 第一年
		Integer years2 = years + 1;// 第二年
		Integer years3 = years2 + 1;// 第三年
		Integer years4 = years3 + 1;// 第四年
		List<Schoolyear> sc = null;
		Specification<Schoolyear> spec = new Specification<Schoolyear>() {
			@Override
			public Predicate toPredicate(Root<Schoolyear> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				HashSet<Predicate> rules = new HashSet<>();
				Predicate predicate = null;
				rules.add(criteriaBuilder.like(root.get("xnxq"), years + "年秋季学期" + "%"));

				rules.add(criteriaBuilder.like(root.get("xnxq"), years2 + "%"));

				rules.add(criteriaBuilder.like(root.get("xnxq"), years3 + "%"));

				rules.add(criteriaBuilder.like(root.get("xnxq"), years4 + "年春季学期" + "%"));

				predicate = criteriaBuilder.or(rules.toArray(new Predicate[rules.size()]));
				return predicate;
			}
		};
		Sort sort = new Sort(Sort.Direction.DESC, "id");
		sc = schoolyerarService.findAll(spec, sort);
		return sc;
	}

	@RequestMapping(value = "/getBanji")
	@ResponseBody
	private Object getBanji(String year,String college,String profesion) {
		Profession profession = professionService.findByYearAndCollegeAndProfesion(year, college, profesion);
		List<TbClass> list = tbClassService.findByprofessionID(profession.getId());
		if (list == null) {
			return null;
		}
		return list;
	}

	@RequestMapping("/getEp")
	@ResponseBody
	public List<EducationalPlanner> getKc() {
		List<EducationalPlanner> epData = educationalPlannerService.findAll();
		return epData;
	}

	// 获取所有教师
	@RequestMapping(value = "/getTeacher")
	@ResponseBody
	public Object getTeacher() {
		List<Teacher> list = teacherService.findAll();
		return list;
	}

	@Override
	public void manage(ModelMap map) {
		List<String> findAllYear = proservice.findAllYear();
		map.put("year", findAllYear);
	}

	
	  @Override 
	  public Specification<Schedule> buildSpec(ScheduleForm form) {
		  Specification<Schedule> spec = new Specification<Schedule>() {
		  
		  @Override 
		  public Predicate toPredicate(Root<Schedule> root, CriteriaQuery<?>query, CriteriaBuilder cb) {
				  HashSet<Predicate> rules = new HashSet<>(); 
				  if(StringUtils.hasText(form.getYear())) {
			        rules.add(cb.like(root.get("profession").get("year"), "%" + form.getYear() +"%")); 
			      } 
				  if (StringUtils.hasText(form.getCollege())) {
			        rules.add(cb.like(root.get("profession").get("profesion"), "%" +form.getCollege() + "%")); 
			      } 
			      if (StringUtils.hasText(form.getXnxq())) {
			          rules.add(cb.like(root.get("xnxq"), "%" + form.getXnxq() + "%")); 
			      }if (StringUtils.hasText(form.getMajor())) {
			          rules.add(cb.like(root.get("tbclass"), "%" + form.getMajor() + "%")); 
			      } if(StringUtils.hasText(form.getSearchTeacher())) {
			          rules.add(cb.equal(root.get("teacher"), form.getSearchTeacher())); 
			      } if(StringUtils.hasText(form.getSearch())) {
			    	  Predicate kcmcname =cb.like(root.get("kcmc"), "%" + form.getSearch() + "%"); 
			    	  Predicate teachername = cb.like(root.get("teacher"), "%" + form.getSearch() + "%");
			          Predicate jcsj = cb.like(root.get("jcsj"), "%" + form.getSearch() + "%");
			          Predicate khfs = cb.like(root.get("khfs"), "%" + form.getSearch() + "%");
			          Predicate serialNumber = cb.like(root.get("serialNumber"), "%" +form.getSearch() + "%"); 
			          Predicate rules2 = cb.or(kcmcname, teachername,jcsj, khfs, serialNumber); rules.add(rules2); 
			      } 
			      return cb.and(rules.toArray(new Predicate[rules.size()])); 
			      }
		  
		      };
		      return spec; 
	      }
	 

	/**
	 * 生成授课记录/流水表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/generateTab")
	@ResponseBody
	public Object generateUp(String startTime, String endTime) {
		try {
			List<Schedule> allData = scheduleService.findAll();
			List<FlowTable> allFlow = flowTableService.findAll();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); // 设置日期格式
			String dqTime = df.format(new Date()); // 当前时间
			String yearStr = dqTime.substring(0, 4); // 当前年份
			int year = Integer.parseInt(yearStr);
			int dqYear = year;
			String month = dqTime.substring(5, dqTime.length() - 3); // 当前月份
			String term = monthToTerm(month); // 获取学期
			if (term.equals("第一学期")) { // 第一学期获取开学时间应该减少一年(因为存在1月这个特殊情况)
				year--;
			}
			if (term.equals("第二学期")) {
				dqYear--;
			}
			Date beginDate = null; // 开始时间
			Date endDate = null; // 结束时间
			List<Xqsj> xqTime = xqsjService.findAll();
			for (Xqsj xq : xqTime) { // 找到当前年份对应的开学时间以及结束时间
				String str = xq.getXqks().substring(0, 4);
				if (str.equals("" + year + "")) {
					beginDate = df.parse(xq.getXqks());
					endDate = df.parse(xq.getXqjs());
					break;
				}
			}
			System.out.println("开学时间:"+df.format(beginDate));
			System.out.println("放假时间:"+df.format(endDate));
			if (beginDate != null && endDate != null) {
				if (startTime != null && endTime != null) {
					if (beginDate.before(df.parse(startTime)) && endDate.after(df.parse(endTime))) {
						beginDate = df.parse(startTime);
						endDate = df.parse(endTime);
					} else {
						return "所选时间不在在校时间内！";
					}
				}
				Date date = beginDate;
				Calendar c = Calendar.getInstance();
				while (!date.equals(endDate)) { // 先删除已存在的流水表
					for (FlowTable f : allFlow) {
						Date parse = df.parse(f.getTime());
						if (date.equals(parse)) {
							flowTableService.delete(f);
						}
					}
					c.setTime(date);
					c.add(Calendar.DATE, 1); // 日期加1天
					date = c.getTime();
				}
				date = beginDate;
				while (!date.equals(endDate)) {
					String week = dateToWeek(df.format(date));// 当前日期(date)对应的星期
					for (Schedule cs : allData) {
						int csYear = Integer.parseInt(cs.getProfession().getYear());// 数据中课程对应的年级
						if (yearToTerm(csYear, dqYear, month).equals(cs.getKkxq())) { // 根据年级获得学期，学期对应才生成
							int beginzc = Integer.parseInt(cs.getQszc()); // 开始周次
							int endzc = Integer.parseInt(cs.getJszc()); // 结束周次
							int zc = dateToZc(df.format(beginDate), df.format(date)); // 当前周次
							if (zcUtils(beginzc, endzc, zc)) { // 周次对应才生成
								if (cs.getXq().equals(week)) { // 星期对应才生成
									FlowTable flow = new FlowTable();
									/*
									 * String classes = ""; Integer number = 0; for (TbClass t : cs.getTbclass()) {
									 * classes += t.getName() + ","; number += t.getNumber(); }
									 */
									flow.setClasses(cs.getTbclass());
									flow.setCourse(cs.getKcmc());
									flow.setClassRoom(cs.getSerialNumber());
									flow.setTeacher(cs.getTeacher());
									flow.setZxs(cs.getZongxs());
									flow.setTime(df.format(date));
									flow.setWeek(week);
									flow.setJc(cs.getJcsj());
									String[] classes = cs.getTbclass().split(",");
									int bjrs = 0;
									for (String s : classes) {
										TbClass bj = tbClassService.findByNameAndProfession(cs.getProfession().getId(),
												s);
										bjrs += bj.getNumber();
									}
									flow.setYdrs(bjrs);
									Classroom classroom = classroomService.findBySerialNumber(cs.getSerialNumber());
									flow.setLd(classroom.getLocation().getName());
									flow.setLc(classroom.getFloor());
									/*
									 * String kclbs = ""; for (CourseCategory category :
									 * cs.getEp().getCurriculum().getKclbs()) { kclbs += category.getType() + ","; }
									 * flow.setKclbs(kclbs.substring(0, kclbs.length() - 1));
									 */
									flow.setKclbs(cs.getKclb());
									flow.setYear(cs.getProfession().getYear());
									flow.setTerm(cs.getXnxq());
									flow.setXmh(cs.getXmh());
									flowTableService.save(flow);
								}
							}

						}

					}
					c.setTime(date);
					c.add(Calendar.DATE, 1); // 日期加1天
					date = c.getTime();
				}
			} else {
				System.out.println("当年开学时间或结束时间未指定");
				return "当年开学时间或结束时间未指定！";
			}

		} catch (Exception e) {
			return "error";
		}
		return "ok";
	}

	/**
	 * 
	 * @param beginzc 开始周次
	 * @param endzc   结束周次
	 * @param zc      需判断的周次
	 * @return 周次在开始-结束周次之内返回1，否则0
	 */
	private boolean zcUtils(int beginzc, int endzc, int zc) {
		int[] list = new int[endzc - beginzc];
		for (int i = 0; i < list.length; i++) {
			list[i] = beginzc + i;
		}
		for (int i = 0; i < list.length; i++) {
			if (zc == list[i]) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 日期转星期
	 * 
	 * @param datetime
	 * @return
	 */
	public static String dateToWeek(String datetime) {
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance(); // 获得一个日历
		Date datet = null;
		try {
			datet = f.parse(datetime);
			cal.setTime(datet);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	/**
	 * 根据年级判断学期
	 * 
	 * @param year   待测年级(如"2017"级)
	 * @param dqYear 当前年份
	 * @param month  当前月份
	 * @return
	 */
	public static String yearToTerm(int year, int dqYear, String month) {
		/*
		 * SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); String dqTime =
		 * df.format(new Date()); // 当前时间 String dqYearStr = dqTime.substring(0, 4); //
		 * 当前年份 int dqYear = Integer.parseInt(dqYearStr); // 当前年份 dqYear--; String month
		 * = dqTime.substring(5, dqTime.length() - 3); // 当前月份
		 */
		if (dqYear == year) {
			String term = monthToTerm(month);
			if ("第一学期".equals(term)) {
				return "一";
			}
			if ("第二学期".equals(term)) {
				return "二";
			}
		}
		if (dqYear - 1 == year) {
			String term = monthToTerm(month);
			if ("第一学期".equals(term)) {
				return "三";
			}
			if ("第二学期".equals(term)) {
				return "四";
			}
		}
		if (dqYear - 2 == year) {
			String term = monthToTerm(month);
			if ("第一学期".equals(term)) {
				return "五";
			}
			if ("第二学期".equals(term)) {
				return "六";
			}
		}

		return "无对应的学期，年级给错了？？";

	}

	/**
	 * 根据月份判断学期
	 * 
	 * @param month 要判断的月份
	 * @return
	 */
	public static String monthToTerm(String month) {
		String[] one = { "01", "02", "03", "04", "05", "06" };
		String[] two = { "07", "08", "09", "10", "11", "12" };

		for (String s : one) {
			if (month.equals(s)) {
				return "第二学期";
			}
		}
		for (String s : two) {
			if (month.equals(s)) {
				return "第一学期";
			}
		}
		return "无对应学期，月份给错了？？？";

	}

	/**
	 * 根据开学时间判断某时间对应的周次
	 * 
	 * @param beginDateStr 开学时间
	 * @param flagDateStr  要判断的时间
	 * @return 要判断的时间对应的周次
	 */
	public static int dateToZc(String beginDateStr, String flagDateStr) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date keyDate, beginDate, endDate;
		try {
			keyDate = null;
			beginDate = df.parse(beginDateStr);
			endDate = df.parse(flagDateStr);
			String week = dateToWeek(beginDateStr);
			Calendar c = Calendar.getInstance();
			switch (week) {
			case "星期一":
				keyDate = beginDate;
				break;
			case "星期二":
				c.setTime(beginDate);
				c.add(Calendar.DATE, -1);
				keyDate = c.getTime();
				break;
			case "星期三":
				c.setTime(beginDate);
				c.add(Calendar.DATE, -2);
				keyDate = c.getTime();
				break;
			case "星期四":
				c.setTime(beginDate);
				c.add(Calendar.DATE, -3);
				keyDate = c.getTime();
				break;
			case "星期五":
				c.setTime(beginDate);
				c.add(Calendar.DATE, -4);
				keyDate = c.getTime();
				break;
			case "星期六":
				c.setTime(beginDate);
				c.add(Calendar.DATE, -5);
				keyDate = c.getTime();
				break;
			case "星期日":
				c.setTime(beginDate);
				c.add(Calendar.DATE, -6);
				keyDate = c.getTime();
				break;
			default:
				System.out.println("应该不存在此处错误，试着检查日期格式是否正确？");
				break;
			}

			int result = 0;
			c.setTime(df.parse(flagDateStr));
			int day1 = c.get(Calendar.DAY_OF_YEAR);
			c.setTime(keyDate);
			int day2 = c.get(Calendar.DAY_OF_YEAR);
			result = ((day1 - day2) / 7) + 1;
			return result;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;

	}

	/**
	 * 导出课程表
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/export")
	@ResponseBody
	public void export(HttpServletRequest request, HttpServletResponse response) {
		List<Schedule> list = scheduleService.findAll();
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setWrapText(true);
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		HSSFFont font = workbook.createFont();
		font.setFontName("楷体_GB2312");
		cellStyle.setFont(font);
		HSSFSheet sheet = workbook.createSheet("课程表");
		String[] fieldnames = { "课程名称", "班级", "教师", "周次", "星期", "节次", "地点", "上课人数", "年级", "专业" };
		HSSFRow row = sheet.createRow(0);
		for (int i = 0; i < fieldnames.length; i++) {
			HSSFCell cell = row.createCell(i);
			cell.setCellValue(fieldnames[i]);
			cell.setCellStyle(cellStyle);
		}

		for (int i = 0; i < list.size(); i++) {
			row = sheet.createRow(i + 1);
			Schedule s = list.get(i);
			HSSFCell cell = row.createCell(0);
			cell.setCellValue(s.getKcmc());
			cell.setCellStyle(cellStyle);
			cell = row.createCell(1);
			cell.setCellValue(s.getTbclass());
			cell.setCellStyle(cellStyle);
			cell = row.createCell(2);
			cell.setCellValue(s.getTeacher());
			cell.setCellStyle(cellStyle);
			cell = row.createCell(3);
			cell.setCellValue(s.getQszc());
			cell.setCellStyle(cellStyle);
			cell = row.createCell(4);
			cell.setCellValue(s.getXq());
			cell.setCellStyle(cellStyle);
			cell = row.createCell(5);
			cell.setCellValue(s.getJcsj());
			cell.setCellStyle(cellStyle);
			cell = row.createCell(6);
			cell.setCellValue(s.getSerialNumber());
			cell.setCellStyle(cellStyle);
			cell = row.createCell(7);
			cell.setCellValue(s.getCapacity());
			cell.setCellStyle(cellStyle);
			cell = row.createCell(8);
			cell.setCellValue(s.getProfession().getYear());
			cell.setCellStyle(cellStyle);
			cell = row.createCell(9);
			cell.setCellValue(s.getProfession().getProfesion());
			cell.setCellStyle(cellStyle);
		}
		String filename = "Schedule.xls";
		response.setHeader("Content-disposition", "attachment; filename=" + filename);
		response.setContentType("application/msexcel");
		ServletOutputStream outputStream;
		try {
			outputStream = response.getOutputStream();
			workbook.write(outputStream);
			workbook.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
